﻿using BJ.AuthDomain.IService;
using BJ.AuthEntity.Models;
using BJ.AuthEntity.ViewModels;
using Syspetro.Core.DbAccessor;
using Syspetro.Deploy.FreeSqlDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mapster;
using Syspetro.Deploy.Managers;

namespace BJ.AuthDomain.Service
{
    public class MenuService : EntityBaseService<Menu>, IMenuService
    {
        readonly IRepository<Menu> repository;
        readonly IAuthorizationManager _authorization;
        readonly IFreeSql _freeSql;
        public MenuService(IFreeSql freeSql, IAuthorizationManager authorization)
        {
            _freeSql = freeSql;
            _authorization = authorization;
            this.repository = new FreeSqlRepository<Menu>(freeSql);
        }


        public override IRepository<Menu> Repository
        {
            get
            {
                return repository;
            }
        }

        public async Task<List<VM_Menu>> FindMenusAsTreeCte()
        {
            var config = new TypeAdapterConfig();
            //映射规则
            config.ForType<Menu, VM_Menu>()
                .Map(dest => dest.ParentName, src => src.Parent.Name);
            var ss = await _freeSql.Select<Menu>().OrderBy(d => d.Sort).ToTreeListAsync();
            return ss.Adapt<List<VM_Menu>>(config);
        }
        public async Task<List<VM_RoleMenu>> GetRoleMenus(Guid roleId)
        {
            if (roleId == Guid.Empty)
                return null;
            var roleMenus = await _freeSql.Select<MenuAuth>().Where(d => d.RoleId == roleId).ToListAsync();

            var config = new TypeAdapterConfig();
            //映射规则
            config.ForType<Menu, VM_RoleMenu>()
                .Map(dest => dest.Checked, src => roleMenus.Any(d => d.MenuId == src.Id) ? true : false);
            var ss = await _freeSql.Select<Menu>().OrderBy(d => d.Sort).ToTreeListAsync();

            return ss.Adapt<List<VM_RoleMenu>>(config);
        }
        public List<VM_RoleMenu> CheckedNodes(VM_RoleMenu parent, List<VM_RoleMenu> checkednodes)
        {

            VM_RoleMenu node = parent;
            if (node != null)
            {
                if (node.Checked == true)
                    checkednodes.Add(node);

                if (node.Children != null)////如果node节点还有子节点则进入遍历
                {
                    CheckedNodes(node, checkednodes);
                }

            }

            return checkednodes;
        }

        public async Task<bool> AuthMenus(VM_AuthMenus authMenus)
        {
            if (authMenus.Uid == Guid.Empty)
                authMenus.Uid = _authorization.GetUserId;
            List<MenuAuth> MenuAuths = new();

            foreach (var mid in authMenus.Menus)
            {
                if (mid.Checked)
                {

                    MenuAuths.Add(new MenuAuth { MenuId = mid.Id, RoleId = authMenus.Uid });
                    if (mid.Children != null && mid.Children.Count(d => d.Checked) > 0)
                    {
                        var checkChd = mid.Children.Where(d => d.Checked).ToList();
                        checkChd.ForEach(d =>
                        {
                            MenuAuths.Add(new MenuAuth { MenuId = d.Id, RoleId = authMenus.Uid });
                        });
                    }

                }
            }
            var ma = _freeSql.GetRepository<MenuAuth>();
            ma.Delete(t => t.RoleId == authMenus.Uid);
            await ma.InsertAsync(MenuAuths);
            return true;
        }
    }
}
